//
//  MJDiscoverView.swift
//  新浪微博
//
//  Created by MaJi on 16/4/6.
//  Copyright © 2016年 MaJi. All rights reserved.
//

import UIKit

class MJDiscoverView: UIView {

    @IBOutlet weak var cancelBtn: UIButton!
    
    @IBOutlet weak var discoverText: UITextField!

    @IBOutlet weak var rightCons: NSLayoutConstraint!

    override func awakeFromNib() {
//        1.设置大小
        self.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: self.frame.height)
//        2.设置discoverText的leftView
        self.discoverText.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        self.discoverText.leftView?.frame = CGRect(x: 0, y: 0, width: self.discoverText.bounds.height, height: self.discoverText.bounds.height)
//        3.设置图片显示模式
        self.discoverText.leftView?.contentMode = .Center
//        4.设置图片总是显示
        self.discoverText.leftViewMode = .Always
        
//        5.设置文本框的圆角
//        self.discoverText.layer.cornerRadius = 5
//        self.discoverText.layer.masksToBounds = true
//        self.discoverText.layer.borderColor
        
    }
    //定义一个类方法 有返回值的类方法
   class func loadSearchView() -> MJDiscoverView{
       let searchView =  NSBundle.mainBundle().loadNibNamed("MJDiscoverView", owner: nil, options: nil).last as! MJDiscoverView
        
        return searchView
    }
    //自动布局的动画执行
//    1.修改约束
//    2.在动画闭包中强制刷新页面
    @IBAction func discoverTextEdit(sender: AnyObject) {
        rightCons.constant = cancelBtn.frame.width
        
        UIView.animateWithDuration(0.3) { () -> Void in
            self.layoutIfNeeded()
        }
    }
    
    
    @IBAction func cancelBtnDidClick(sender: AnyObject) {
//        取消键盘
        discoverText.resignFirstResponder()
        rightCons.constant = 0
        
        UIView.animateWithDuration(0.3) { () -> Void in
            self.layoutIfNeeded()
        }

    }
}
